home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-21 | 15.3 KB | 534 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: TextSelc.cpp
- // Release Version: $ 1.0d1 $
- //
- // Author: Anthone Burbidge
- // Creation Date: 3/28/94
- //
- // Copyright: © 1993, 1994 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef _TEXTPART_
- #include "TextPart.h"
- #endif
-
- #ifndef _TEXTSELC_
- #include "TextSelc.h"
- #endif
-
- #ifndef _VALUESTREAM_
- #include "ValueStream.h"
- #endif
-
- #ifndef _TEXTUTIL_
- #include "TextUtil.h"
- #endif
-
- #ifndef _TEXTFACET_
- #include "TextFacet.h"
- #endif
-
- #ifndef _TEXTPROXY_
- #include "TextProxy.h"
- #endif
-
- #ifndef _EMBEDRUN_
- #include "EmbedRun.h"
- #endif
-
- #ifndef _ODTEXT_
- #include "ODText.h"
- #endif
-
- #ifndef FWRECT_H
- #include "FWRect.h"
- #endif
-
- // ----- Framework Includes -----
-
- #ifndef FWMEMMGR_H
- #include <FWMemMgr.h>
- #endif
-
- #ifndef FWFRAME_H
- #include "FWFrame.h"
- #endif
-
- // ----- Textension Includes -----
-
- #ifndef _TextensionCommon_
- #include "TextensionCommon.h"
- #endif
-
- #ifndef _Textension_
- #include "Textension.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef _STDPROPS_
- #include <StdProps.h>
- #endif
-
- #ifndef _TRNSFORM_
- #include <TrnsForm.h>
- #endif
-
- #ifndef _STDTYPES_
- #include <StdTypes.h>
- #endif
-
- #ifndef _SHAPE_
- #include <Shape.h>
- #endif
-
- #ifndef _TRANSLAM_
- #include <TranslaM.h>
- #endif
-
- #ifndef _XMPSESSM_
- #include <XMPSessM.h>
- #endif
-
- // ----- Macintosh Includes -----
-
- #ifndef __FIXMATH__
- #include <FixMath.h>
- #endif
-
- #ifndef mathRoutinesIncludes
- #include <Math Routines.h>
- #endif
-
- #pragma segment TextPartSegment
-
- //========================================================================================
- // CLASS CTextSelection
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CTextSelection::CTextSelection
- //----------------------------------------------------------------------------------------
-
- CTextSelection::CTextSelection():
- fTextPart(NULL),
- fIsCloning(FALSE),
- fCommandFrame(NULL)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CTextSelection::InitTextSelection
- //----------------------------------------------------------------------------------------
-
- void CTextSelection::InitTextSelection(CTextPart* textPart)
- {
- InitSelection(textPart, TRUE, TRUE);
- fTextPart = textPart;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextSelection::~CTextSelection
- //----------------------------------------------------------------------------------------
-
- CTextSelection::~CTextSelection()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CTextSelection::CloseSelection
- //----------------------------------------------------------------------------------------
-
- void CTextSelection::CloseSelection()
- {
- CTextFacet* facet = fTextPart->GetAnyFacet(fTextPart->GetMainPresentation());
-
- CTextDrawInitiator di((CTextFacet *) facet);
- TOffsetRange selRange((long) 0, (long) 0);
- fTextPart->GetEditText()->SetSelectionRange(selRange);
- }
-
- //----------------------------------------------------------------------------------------
- // CTextSelection::SelectAll
- //----------------------------------------------------------------------------------------
-
- void CTextSelection::SelectAll()
- {
- TOffsetRange selRange(0, fTextPart->GetEditText()->CountChars());
- fTextPart->GetEditText()->SetSelectionRange(selRange);
- }
-
- //----------------------------------------------------------------------------------------
- // CTextSelection::IsEmpty
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CTextSelection::IsEmpty() const
- {
- TOffsetRange range;
- fTextPart->GetEditText()->GetSelectionRange(&range);
-
- return range.IsEmpty();
- }
-
- //----------------------------------------------------------------------------------------
- // CTextSelection::DoClear
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CTextSelection::DoClear()
- {
- CTextFacet* facet = fTextPart->GetAnyFacet(fTextPart->GetMainPresentation());
- CTextDrawInitiator di(facet);
-
- TOffsetRange range;
- fTextPart->GetEditText()->GetSelectionRange(&range);
-
- TReplaceParams replaceParams;
- fTextPart->GetEditText()->ReplaceRange(range.Start(), range.End(),
- &replaceParams, false);
-
- fTextPart->InvalidateAllFrames(fTextPart->GetMainPresentation(), facet, NULL);
-
- return TRUE;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextSelection::IsSelectionOnlyOneProxyRun
- //----------------------------------------------------------------------------------------
-
- FW_CProxyRun* CTextSelection::IsSelectionOnlyOneProxyRun() const
- {
- FW_CProxyRun* proxyRun = NULL;
- CRunsRanges* runsRanges = fTextPart->GetEditText()->GetRunsRanges();
-
- TOffsetRange range;
- fTextPart->GetEditText()->GetSelectionRange(&range);
-
- if (range.Len() == 1)
- {
- long runLength;
- CRunObject* run;
-
- runsRanges->GetNextRun(range.Start(), &run, &runLength);
- if (run->GetClassId() == kEmbeddedPartClassId)
- proxyRun = ((CEmbeddedRun *) run)->GetProxyRun();
- }
-
- return proxyRun;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextSelection::EmbedSelection
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CTextSelection::EmbedSelection(XMPPart* xmpPart, XMPShape* frameShape)
- {
- FW_CRect shapeRect;
- if (frameShape)
- {
- frameShape->GetBoundingBox(&shapeRect);
- shapeRect.Offset(ff(-shapeRect.left), ff(-shapeRect.top));
- }
- else
- {
- shapeRect.Set(ff(0), ff(0), ff(80), ff(80));
- }
-
- fTextPart->HandleEmbedPart(xmpPart, shapeRect);
-
- return TRUE;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextSelection::ExternalizeSelection_ContentPropertyValueType
- //----------------------------------------------------------------------------------------
-
- void CTextSelection::ExternalizeSelection_ContentPropertyValueType
- (XMPStorageUnit* storageUnit, FW_CFrame* commandFrame, XMPCloneKind cloneKind)
- {
- FW_UNUSED(cloneKind);
-
- storageUnit->AddValue(this->GetPart()->GetContentPropertyValueType());
-
- CValueStream *stream = new CValueStream;
- stream->InitValueStream(storageUnit);
-
- CTextensionIOSuite *ioSuite = new CTextensionIOSuite;
- ioSuite->ITextensionIOSuite(false, stream);
-
- CTextPart::gCurrentPart = fTextPart;
- fIsCloning = TRUE;
- fCommandFrame = commandFrame;
-
- TOffsetRange range2Save;
- fTextPart->GetEditText()->GetSelectionRange(&range2Save);
- OSErr err = fTextPart->GetEditText()->GetRangeIOSuite(&range2Save, ioSuite);
-
- fCommandFrame = NULL;
- fIsCloning = FALSE;
- CTextPart::gCurrentPart = NULL;
-
- ioSuite->Free();
- delete ioSuite;
-
- stream->Free();
- delete stream;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextSelection::ExternalizeSelection_TEXT
- //----------------------------------------------------------------------------------------
-
- void CTextSelection::ExternalizeSelection_TEXT(XMPStorageUnit* storageUnit,
- FW_CFrame* commandFrame,
- XMPCloneKind cloneKind)
- {
- FW_UNUSED(cloneKind);
- FW_UNUSED(commandFrame);
-
- XMPTranslation *translate = GetPart()->GetSession()->GetTranslation();
- XMPType kAppleTEXT = translate->GetISOTypeFromPlatformType('TEXT', kXMPPlatformDataType);
-
- storageUnit->AddValue(kAppleTEXT);
-
- CValueStream *stream = new CValueStream;
- stream->InitValueStream(storageUnit);
-
- CTextensionIOSuite *ioSuite = new CTextensionIOSuite;
- ioSuite->ITextensionIOSuite(false, stream);
-
- TOffsetRange range2Save;
- fTextPart->GetEditText()->GetSelectionRange(&range2Save);
- OSErr err = fTextPart->GetEditText()->GetRangeIOSuite(&range2Save, ioSuite, kIOText);
-
- ioSuite->Free();
- delete ioSuite;
-
- stream->Free();
- delete stream;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextSelection::ExternalizeSelection
- //----------------------------------------------------------------------------------------
-
- void CTextSelection::ExternalizeSelection(XMPStorageUnit* storageUnit,
- FW_CFrame* commandFrame, XMPCloneKind cloneKind)
- {
- storageUnit->AddProperty(kXMPPropContents);
-
- this->ExternalizeSelection_ContentPropertyValueType(storageUnit, commandFrame, cloneKind);
- this->ExternalizeSelection_TEXT(storageUnit, commandFrame, cloneKind);
- }
-
- //----------------------------------------------------------------------------------------
- // CTextSelection::InternalizeSelection_ContentPropertyValueType
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CTextSelection::InternalizeSelection_ContentPropertyValueType
- (XMPStorageUnit* storageUnit, XMPCloneKind cloneKind)
- {
- FW_UNUSED(cloneKind);
-
- // ----- Focus on content property -----
- storageUnit->Focus(kXMPPropContents, kXMPPosUndefined,
- this->GetPart()->GetContentPropertyValueType(),
- (XMPValueIndex)0, kXMPPosUndefined);
-
- CTextFacet* facet = fTextPart->GetAnyFacet(fTextPart->GetMainPresentation());
- CTextDrawInitiator di(facet);
-
- CValueStream *stream = new CValueStream;
- stream->InitValueStream(storageUnit);
-
- CTextensionIOSuite *ioSuite = new CTextensionIOSuite;
- ioSuite->ITextensionIOSuite(false, stream);
-
- CTextPart::gCurrentPart = fTextPart;
- fIsCloning = TRUE;
-
- TOffsetRange range2Replace;
- fTextPart->GetEditText()->GetSelectionRange(&range2Replace);
- TReplaceParams replaceParams(ioSuite);
- OSErr err = fTextPart->GetEditText()->ReplaceRange(range2Replace.Start(),
- range2Replace.End(),
- &replaceParams, false);
-
- fTextPart->InvalidateAllFrames(fTextPart->GetMainPresentation(), facet, NULL);
-
- fIsCloning = FALSE;
- CTextPart::gCurrentPart = NULL;
-
- ioSuite->Free();
- delete ioSuite;
-
- stream->Free();
- delete stream;
-
- return TRUE;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextSelection::InternalizeSelection_TEXT
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CTextSelection::InternalizeSelection_TEXT
- (XMPStorageUnit* storageUnit, XMPCloneKind cloneKind)
- {
- FW_UNUSED(cloneKind);
-
- XMPTranslation *translate = GetPart()->GetSession()->GetTranslation();
- XMPType kAppleTEXT = translate->GetISOTypeFromPlatformType('TEXT', kXMPPlatformDataType);
-
- storageUnit->Focus(kXMPPropContents, kXMPPosUndefined, kAppleTEXT,
- (XMPValueIndex)0, kXMPPosUndefined);
-
- CTextFacet* facet = fTextPart->GetAnyFacet(fTextPart->GetMainPresentation());
- CTextDrawInitiator di(facet);
-
- unsigned long textSize = storageUnit->GetSize();
- FW_PlatformHandle textHandle = FW_CMemoryManager::AllocateSystemHandle(textSize);
-
- FW_CMemoryManager::LockSystemHandle(textHandle);
- storageUnit->GetValue(textSize, (XMPValue)*textHandle);
-
- TOffsetRange range2Replace;
- fTextPart->GetEditText()->GetSelectionRange(&range2Replace);
- TReplaceParams replaceParams(TTextDescriptor((unsigned char *) *textHandle, textSize));
- OSErr err = fTextPart->GetEditText()->ReplaceRange(range2Replace.Start(),
- range2Replace.End(),
- &replaceParams, false);
-
- fTextPart->InvalidateAllFrames(fTextPart->GetMainPresentation(), facet, NULL);
-
- FW_CMemoryManager::UnlockSystemHandle(textHandle);
- FW_CMemoryManager::FreeSystemHandle(textHandle);
-
- return TRUE;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextSelection::InternalizeSelection
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CTextSelection::InternalizeSelection(XMPStorageUnit* storageUnit,
- XMPCloneKind cloneKind)
- {
- FW_UNUSED(cloneKind);
-
- FW_Boolean success = FALSE;
-
- XMPTranslation *translate = GetPart()->GetSession()->GetTranslation();
- XMPType kAppleTEXT = translate->GetISOTypeFromPlatformType('TEXT', kXMPPlatformDataType);
-
- if (storageUnit->Exists(kXMPPropContents, this->GetPart()->GetContentPropertyValueType(), 0))
- success = this->InternalizeSelection_ContentPropertyValueType(storageUnit, cloneKind);
- else if (storageUnit->Exists(kXMPPropContents, kAppleTEXT, 0))
- success = this->InternalizeSelection_TEXT(storageUnit, cloneKind);
-
- return success;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextSelection::CalcDragRgn
- //----------------------------------------------------------------------------------------
-
- XMPRgnHandle CTextSelection::CalcDragRgn(FW_CFacet* facet)
- {
- FW_UNUSED(facet);
-
- // Just temporary until I get a Textension that provides me with an easy method to
- // get the region of the current selection.
-
- TOffsetRange selection;
- fTextPart->GetEditText()->GetSelectionRange(&selection);
-
- long top, trailEdge;
- short hite;
- FW_SPlatformRect rt;
-
- fTextPart->GetEditText()->Char2Point(selection.Start(), &top, &trailEdge, &hite);
-
- rt.top = (short) top;
- rt.left = (short) trailEdge;
-
- fTextPart->GetEditText()->Char2Point(selection.End(), &top, &trailEdge, &hite);
-
- rt.bottom = (short) top + hite;
- rt.right = (short) trailEdge;
-
- XMPRgnHandle dragRgn = ::NewRgn();
- FW_PlatformRegion tempRgn = ::NewRgn();
-
- if (selection.Len() > 0)
- {
- ::RectRgn(dragRgn, &rt);
- ::CopyRgn(dragRgn, tempRgn);
- ::InsetRgn(tempRgn, 1, 1);
- ::DiffRgn(dragRgn, tempRgn, dragRgn);
- }
-
- ::DisposeRgn(tempRgn);
-
- return dragRgn;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextSelection::DoDrop
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CTextSelection::DoDrop(XMPStorageUnit* dropSU, FW_CFacet* facet,
- const FW_CPoint& originPoint, const FW_CPoint& dropPoint)
- {
- // Set the Textension selection
-
- TOffsetRange range;
- Boolean outside;
-
- FW_CPoint where = facet->GetWindowContentTransform()->InvertPoint(dropPoint);
- FW_SPlatformPoint pt;
- where.AsPlatformPoint(pt);
-
- CTextDrawInitiator di((CTextFacet *) facet);
-
- fTextPart->GetEditText()->Point2Char(pt, &range, &outside);
- fTextPart->GetEditText()->SetSelectionRange(range);
-
- return FW_CSelection::DoDrop(dropSU, facet, originPoint, dropPoint);
- }
-
- //----------------------------------------------------------------------------------------
- // CTextSelection::DoDroppedInSameFrame
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CTextSelection::DoDroppedInSameFrame(XMPStorageUnit* dropSU, FW_CFacet* facet,
- const FW_CPoint& originPoint, const FW_CPoint& dropPoint)
- {
- return this->DoDrop(dropSU, facet, originPoint, dropPoint);
- }
-
- //----------------------------------------------------------------------------------------
- // CTextSelection::IsPointInSelection
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CTextSelection::IsPointInSelection(const FW_CPoint& xmpPt)
- {
- FW_SPlatformPoint pt;
- xmpPt.AsPlatformPoint(pt);
-
- TOffsetRange selectionRange;
- fTextPart->GetEditText()->GetSelectionRange(&selectionRange);
-
- Boolean outside;
- TOffsetRange charRange;
- fTextPart->GetEditText()->Point2Char(pt, &charRange, &outside);
-
- if (charRange.Start() >= selectionRange.Start()
- && charRange.End() <= selectionRange.End())
- return TRUE;
- else
- return FALSE;
- }
-
-